package com.itheima.d2_xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

class Contact{
    private int id;
    private boolean vip;
    private String name;
    private String gender;
    private String email;

    public Contact() {
    }

    public Contact(int id, boolean vip, String name, String gender, String email) {
        this.id = id;
        this.vip = vip;
        this.name = name;
        this.gender = gender;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean getVip() {
        return vip;
    }

    public void setVip(boolean vip) {
        this.vip = vip;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "contact{" +
                "id='" + id + '\'' +
                ", vip='" + vip + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
public class Demo {
    public static void main(String[] args) throws DocumentException {
        //1.创建contact类型，用来表示联系人数据

        //2.解析xml中的联系人封装成Contact对象，放到集合中

        ArrayList<Contact> list = new ArrayList<>();

        SAXReader saxReader = new SAXReader();

        Document doc = saxReader.read("day11properties-xml-log-thread/xml/contacts.xml");

        Element element = doc.getRootElement();

        List<Element> elements = element.elements();

        for (Element element1 : elements) {
            Contact contact = new Contact();

            String id = element1.attributeValue("id");

            contact.setId(Integer.parseInt(id));

            String vip = element1.attributeValue("vip");
            contact.setVip(Boolean.parseBoolean(vip));

            final String name = element1.elementText("name");
            final String gender = element1.elementText("gender");
            final String email = element1.elementText("email");

            contact.setEmail(email);
            contact.setGender(gender);
            contact.setName(name);

            list.add(contact);



        }
            list.forEach(System.out::println);

    }
}
